﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace config3
{
    internal class FxConfigProvider : FileConfigurationProvider
    {
        public FxConfigProvider(FileConfigurationSource source) : base(source)
        {
        }

        public override void Load(Stream stream)
        {
            var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            XmlDataDocument xml = new XmlDataDocument();
            xml.Load(stream);
            var nlist = xml.SelectNodes("/configuration/connectionStrings/add");
            foreach (XmlNode item in nlist.Cast<XmlNode>())
            {
                string name = item.Attributes["name"].Value;
                string connectionString = item.Attributes["connectionString"].Value;
                data[$"{name}:providerName"] = connectionString;
                var providerName = item.Attributes["providerName"];
                if (providerName != null)
                {
                    data[$"{name}:providerName"] = providerName.Value;
                }
            }
            var alist = xml.SelectNodes("/configuration/appSettings/add");
            foreach (XmlNode item in alist.Cast<XmlNode>())
            {
                string key = item.Attributes["key"].Value;
                string value = item.Attributes["value"].Value;
                key.Replace(".", ":");
                data[key] = value;
            }
            this.Data = data;
        }
    }
}
